FUNCTION YBC_SWS_SEND_MAIL.
*"----------------------------------------------------------------------
*"*"本地接口：
*"  IMPORTING
*"     VALUE(IS_MAIL) TYPE  YBCS_SWS_MAIL
*"  EXPORTING
*"     VALUE(ES_MSG) TYPE  BAPIRET2
*"----------------------------------------------------------------------
  DATA:
    LV_LENGTH             TYPE I,
    LV_DEC                TYPE P DECIMALS 2,
    LV_TIMES              TYPE I,
    LV_INDEX              TYPE I,
    LS_MAIL               TYPE ADR6-SMTP_ADDR,
    LV_SENDER_MAIL        TYPE ADR6-SMTP_ADDR,
    LT_RECEIVER_MAIL      TYPE STANDARD TABLE OF ADR6-SMTP_ADDR,
    LT_CC_MAIL            TYPE STANDARD TABLE OF ADR6-SMTP_ADDR,
    LT_BCC_MAIL           TYPE STANDARD TABLE OF ADR6-SMTP_ADDR,
    LV_ATTACHMENT_SUBJECT TYPE SO_OBJ_DES,
    LS_ATTACHMENT         LIKE LINE OF IS_MAIL-ATTACHMENT,
    REF_SENDER            TYPE REF TO IF_SENDER_BCS,
    REF_BCS               TYPE REF TO CL_BCS,
    REF_DOC_BCS           TYPE REF TO CL_DOCUMENT_BCS,
    REF_RECIPIENT         TYPE REF TO IF_RECIPIENT_BCS,
    LV_SUBJECT            TYPE SO_OBJ_DES,
    LT_SOLI               TYPE SOLI_TAB,
    LS_SOLI               TYPE SOLI,
    LV_STR                TYPE STRING,
    LV_STRING             TYPE STRING,
    REF_MIME_HELPER       TYPE REF TO CL_GBT_MULTIRELATED_SERVICE.

  CLEAR ES_MSG.
  IF IS_MAIL-CONTENT_HTML IS INITIAL AND IS_MAIL-CONTENT_TEXT IS INITIAL.
    ES_MSG-MESSAGE = TEXT-T01.
    ES_MSG-TYPE = 'E'.
    RETURN.
  ENDIF.

  LV_SUBJECT = IS_MAIL-SUBJECT.
  LV_SENDER_MAIL = IS_MAIL-SENDER.
  SPLIT IS_MAIL-RECEIVER AT ';' INTO TABLE LT_RECEIVER_MAIL.
  SPLIT IS_MAIL-CC AT ';' INTO TABLE LT_CC_MAIL.
  SPLIT IS_MAIL-BCC AT ';' INTO TABLE LT_BCC_MAIL.

  IF IS_MAIL-CONTENT_HTML IS NOT INITIAL."以HTML的格式发邮件

    REFRESH LT_SOLI[].
    LV_LENGTH = STRLEN( IS_MAIL-CONTENT_HTML ).
    LV_DEC = LV_LENGTH / 255.
    LV_TIMES = CEIL( LV_DEC ).

    CLEAR LV_INDEX.
    DO LV_TIMES TIMES.
      CLEAR LS_SOLI.
      IF SY-INDEX = LV_TIMES."最后一次循环
        LS_SOLI = IS_MAIL-CONTENT_HTML+LV_INDEX.
      ELSE.
        LS_SOLI = IS_MAIL-CONTENT_HTML+LV_INDEX(255).
      ENDIF.

      LV_INDEX = LV_INDEX + 255.
      APPEND LS_SOLI TO LT_SOLI.
    ENDDO.

  ELSE.
    LT_SOLI = IS_MAIL-CONTENT_TEXT.
  ENDIF.

  TRY.

      IF IS_MAIL-CONTENT_HTML IS NOT INITIAL."以HTML的格式发邮件
        CREATE OBJECT REF_MIME_HELPER.
        CALL METHOD REF_MIME_HELPER->SET_MAIN_HTML
          EXPORTING
            CONTENT     = LT_SOLI
            FILENAME    = 'mail.htm'      "filename for HMTL form
            DESCRIPTION = LV_SUBJECT.  "Title

        "Create HTML using BCS class and attach html and image part to it.
        REF_DOC_BCS = CL_DOCUMENT_BCS=>CREATE_FROM_MULTIRELATED(
          I_SUBJECT = LV_SUBJECT
          I_MULTIREL_SERVICE = REF_MIME_HELPER ).

      ELSE.

        REF_DOC_BCS = CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
          I_TYPE    = 'RAW'
          I_SUBJECT = LV_SUBJECT
          I_TEXT    = LT_SOLI ).

      ENDIF.

      "Send  attachment
      LOOP AT IS_MAIL-ATTACHMENT INTO LS_ATTACHMENT.
        LV_ATTACHMENT_SUBJECT = LS_ATTACHMENT-SUBJECT.
        TRY.
            REF_DOC_BCS->ADD_ATTACHMENT(
              EXPORTING
                I_ATTACHMENT_TYPE = LS_ATTACHMENT-OBJTP
                I_ATTACHMENT_SUBJECT = LV_ATTACHMENT_SUBJECT
                I_ATT_CONTENT_HEX = LS_ATTACHMENT-CONT_HEX ).

          CATCH CX_DOCUMENT_BCS INTO DATA(REF_DOCUMENT_BCS).
            ES_MSG-TYPE = 'E'.
            ES_MSG-MESSAGE = REF_DOCUMENT_BCS->GET_TEXT( ).
            EXIT.
        ENDTRY.
      ENDLOOP.
      IF ES_MSG-TYPE = 'E'.
        RETURN.
      ENDIF.

      REF_BCS = CL_BCS=>CREATE_PERSISTENT( ).
      REF_BCS->SET_DOCUMENT( I_DOCUMENT = REF_DOC_BCS ).

      "Set sender
      IF LV_SENDER_MAIL IS NOT INITIAL.
        REF_SENDER = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( LV_SENDER_MAIL ).
      ELSE.
        REF_SENDER = CL_SAPUSER_BCS=>CREATE( SY-UNAME ).
      ENDIF.

      REF_BCS->SET_SENDER(
        EXPORTING
        I_SENDER = REF_SENDER ).

      LOOP AT LT_RECEIVER_MAIL INTO LS_MAIL.
        REF_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( I_ADDRESS_STRING =  LS_MAIL ).
        REF_BCS->ADD_RECIPIENT( I_RECIPIENT = REF_RECIPIENT ).
      ENDLOOP.

      LOOP AT LT_CC_MAIL INTO LS_MAIL.
        REF_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( I_ADDRESS_STRING =  LS_MAIL ).
        REF_BCS->ADD_RECIPIENT( I_RECIPIENT = REF_RECIPIENT I_COPY = 'X').
      ENDLOOP.

      LOOP AT LT_BCC_MAIL INTO LS_MAIL.
        REF_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( I_ADDRESS_STRING =  LS_MAIL ).
        REF_BCS->ADD_RECIPIENT( I_RECIPIENT = REF_RECIPIENT I_COPY = 'X'  I_BLIND_COPY = 'X' ).
      ENDLOOP.

      "REF_BCS->SET_SEND_IMMEDIATELY( 'X' ).
      REF_BCS->SEND( ).

      COMMIT WORK.
      FREE:REF_RECIPIENT, REF_DOC_BCS, REF_BCS.

      ES_MSG-TYPE = 'S'.
      ES_MSG-MESSAGE = TEXT-T02.

    CATCH CX_BCOM_MIME INTO DATA(REF_CX01).
      FREE:REF_RECIPIENT, REF_DOC_BCS, REF_BCS.
      ES_MSG-TYPE = 'E'.
      ES_MSG-MESSAGE = REF_CX01->GET_TEXT( ).
      RETURN.
    CATCH CX_GBT_MIME INTO DATA(REF_CX02).
      FREE:REF_RECIPIENT, REF_DOC_BCS, REF_BCS.
      ES_MSG-TYPE = 'E'.
      ES_MSG-MESSAGE = REF_CX02->GET_TEXT( ).
      RETURN.
    CATCH CX_BCS INTO DATA(REF_CX03).
      FREE:REF_RECIPIENT, REF_DOC_BCS, REF_BCS.
      ES_MSG-TYPE = 'E'.
      ES_MSG-MESSAGE = REF_CX03->GET_TEXT( ).
      RETURN.
  ENDTRY.

ENDFUNCTION.
